#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;
const int N=1e6+5;
int a[N],b[N],c[N],mark[N],idx;
char s[N];
int main(){
	int t;
	cin>>t;
	while(t--){
		int n;
		idx=0;
		scanf("%d%s",&n,s+1);
		for(int i=1;i<=n;i++){
			a[i]=a[i-1];b[i]=b[i-1];c[i]=c[i-1];
			if(s[i]=='a') a[i]++,mark[++idx]=i;
			if(s[i]=='b') b[i]++;
			if(s[i]=='c') c[i]++;
		}
		int flag=0;
		int res=1e9;
		for(int i=1;i<=idx;i++){
			for(int j=i+1;j<=i+4&&j<=idx;j++){
				int k1=mark[i],k2=mark[j];
				if(k2-k1+1>=res) break;
				if(k2-k1+1>=2&&a[k2]-a[k1-1]>b[k2]-b[k1-1]&&a[k2]-a[k1-1]>c[k2]-c[k1-1]){
					flag=1;
					res=min(res,k2-k1+1);
					break;
				}
			}
		}
		if(flag) printf("%d\n",res);
		else puts("-1");
	}
	return 0;
} 
